////////////////////////////////////////////////////////////////////////////////
//
//  FLYING KIWI BV
//  Copyright 2010 Flying Kiwi BV
//  All Rights Reserved.
//
//  NOTICE: Flying Kiwi permits you to use, modify, and distribute this file
//  in accordance with the terms of the license agreement accompanying it.
//
////////////////////////////////////////////////////////////////////////////////
package plantool.core {
	//--------------------------------------------------------------------------
	//  Imports
	//--------------------------------------------------------------------------
	import flash.events.Event;
	
	import kiwi.display.DisplayObject;
	
	import plantool.api.DateApi;
	import plantool.modules.menu.MenuApi;
	import plantool.modules.orders.OrderApi;
	import plantool.modules.users.UserApi;
	import plantool.modules.workplaces.WorkplaceApi;
	import plantool.data.DatabaseManager;
	import plantool.settings.Settings;
	
	//--------------------------------------------------------------------------
	//  [Metadata (Events, Style, Effects etc)]
	//--------------------------------------------------------------------------
	
	//--------------------------------------------------------------------------
	//  Class declaration
	//--------------------------------------------------------------------------
	public class CoreDisplayObject extends DisplayObject implements ICoreDisplayObject {
		//--------------------------------------------------------------------------
		//Class constants
		//--------------------------------------------------------------------------
		
		//--------------------------------------------------------------------------
		//Class variables
		//--------------------------------------------------------------------------
		
		//--------------------------------------------------------------------------
		//Class properties
		//--------------------------------------------------------------------------
		protected function get databaseManager():DatabaseManager {
			return DatabaseManager.getInstance();
		}
		protected function get dateApi():DateApi {
			return DateApi.getInstance();
		}
		protected function get menuApi():MenuApi {
			return MenuApi.getInstance();
		}
		protected function get orderApi():OrderApi {
			return OrderApi.getInstance();
		}
		protected function get settings():Settings {
			return Settings.getInstance();
		}
		protected function get statusManager():StatusManager {
			return StatusManager.getInstance();
		}
		protected function get userApi():UserApi {
			return UserApi.getInstance();
		}
		protected function get workplaceApi():WorkplaceApi {
			return WorkplaceApi.getInstance();
		}
		
		//--------------------------------------------------------------------------
		//Constructor and Destructor
		//--------------------------------------------------------------------------
		public function CoreDisplayObject() {
			super();
		}
		
		public override function dispose():void {
			//remove render event
			removeEventListener(Event.RENDER	, onRender_object);
			
			//remove from stage
			if (parent != null && parent.contains(this)) {
				parent.removeChild(this);
			}
		}
		
		//--------------------------------------------------------------------------
		//Class methods
		//--------------------------------------------------------------------------
		private function onRender_object(e:Event):void {
			//remove render event
			removeEventListener(Event.RENDER	, onRender_object);
			
			//redraw the object
			draw();
		}
		
		protected override function init():void {
			//invalidate
			invalidate();
		}
		
		public function draw():void {
			
		}
		
		public function invalidate():void {
			//listen to render event
			addEventListener(Event.RENDER	, onRender_object);
			
			//invalidate stage
			base.stage.invalidate();
		}
	}
}